﻿// See https://aka.ms/new-console-template for more information

class Test
{
    static void Main(string[] args)
    {
        //随机创建十个对象
        ShapeFactory sf = new ShapeFactory();
        Shape[] shapes = new Shape[10];
        Random r = new Random();
        int sum = 0;
        for (int i = 0; i < 10; i++)
        {
            int m = r.Next(0, 3);
            string s;
            switch (m)
            {
                case 0:
                    s = "Rectangle";
                    shapes[i] = sf.CreateShape(s);
                    break;
                case 1:
                    s = "Square";
                    shapes[i] = sf.CreateShape(s);
                    break;
                case 2:
                    s = "Triangle";
                    shapes[i] = sf.CreateShape(s);
                    break;
            }
            sum += shapes[i].calArea();
            Console.WriteLine(sum);
        }
       
    }
}
public abstract class Shape
{
    public abstract int calArea();
}

public class Rectangle : Shape 
{
    private int length;

    private int height;
    public Rectangle() { }
    public Rectangle(int length, int height)
    {
        this.length = length;
        this.height = height;
    }
    
    public override int calArea()
    {
        return length * height;
    }
}

class Square : Rectangle
{
    private int length;
   
    public Square(int length) 
    {
        this.length = length;
    }
    public override int calArea()
    {
        return length * length;
    }
}

class Triangle : Shape
{
    int a;
    int b;
    int c;

    public  bool isLegal()
    {
        return (a + b > c && a + c > b && b + c > a);
    }

    public Triangle(int a,int b,int c) 
    { 
        this.a = a;
        this.b = b;
        this.c = c;
    }
    
    public override int calArea()
    {
        if( isLegal())
       {
            int p = (a + b + c) / 2;
            return (int)Math.Sqrt(p * (p - a) * (p - b) * (p - c));
        }
        else
        {
            Console.WriteLine("输入错误");
            return 0;
        }
    }
}

//简单工厂类
public class ShapeFactory
{
    public  Shape CreateShape(string type)
    {
        Shape shape = null;
        Random random = new Random();
       
            int a = random.Next(0,50);
            int b = random.Next(0,50);
            int c = random.Next(0,50);
        
        if (type.Equals("Rectangle"))
        {

            shape = new Rectangle(a,b);
            //Console.WriteLine( "Rectangle"+a+b);
            
        }
        else if (type.Equals("Square"))
        {
            shape = new Square(a);
            //Console.WriteLine("Square"+a );
        }
        else if (type.Equals("Triangle"))
        {
            shape = new Triangle(a,b,c);
            Console.WriteLine("Triangle"+a + "" + b+c);
        }
        else
        {
            Console.WriteLine("无法创建!");
        }
        return shape;
    }
}



